package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.*;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.org.service.IShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private RedisService redisService;
    @Resource
    private SendEmailService emailService;
    @Override
    //加事务
    @Transactional
    public void findStory(Shop shop) {
        //判断密码
        Employee admin = shop.getAdmin();
        if(admin==null){
            throw new BusinessException("管理员信息有误");
        }
        if(!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new BusinessException("2次密码不匹配");
        }
        //查询用户名和邮箱和密码
        List<Employee> employee = employeeMapper.selectUserNameOrPhoneOrEmail(admin);
        //list默认为null集合
        if(employee.size()>0){
            throw new BusinessException("用户的名字和电话和邮箱已经存在");
        }
        /*//设置盐值
        String salt = IdUtil.fastSimpleUUID();
        //记录盐值
        admin.setSalt(salt);
        //加密密码;
        String pwd = MD5.create().setSalt(salt.getBytes()).digestHex(admin.getPassword());
        //把密码设置进去
        admin.setPassword(pwd);*/
        admin.initMD5();
        //插入员工
        employeeMapper.insertSelective(admin);
        //获取员工id并且设置到shop里面
        shop.setAdmin_id(admin.getId());
        //新增商店
        shopMapper.insertSelective(shop);
        //获取新增的shop id放在员工里面然后修改
        admin.setShopId(shop.getId());
        employeeMapper.updateByPrimaryKey(admin);
        //发邮件生成随机吗
        String uuid = IdUtil.fastSimpleUUID();
        //调用方法发邮件
        emailService.send("宠物乐园激活","请激活您的宠物乐园平台店铺,点击链接,http://localhost/shop/active/"+uuid+"/"+shop.getId(),admin.getEmail());
        //设置uuid好获取id
        redisService.setStringKeyAndValue(50,uuid,shop.getId());

    }

    @Override
    //激活
    public Object activeStore(String uuid, String shopId) {
        //根据shopid查数据
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        //判断密码
        if(PetConstant.ShopConstant.ACTIVATION == shop.getState()){
            throw  new BusinessException("店铺已经激活");
        }
        //判断uuid是否存在
        String keyStringValue = redisService.getKeyStringValue(uuid);
        if(StringUtils.isBlank(keyStringValue)){
            throw  new BusinessException("激活时间已过");
        }
        //设置shopid
        shop.setId(Long.valueOf(keyStringValue));
        //改状态
        shop.setState(PetConstant.ShopConstant.ACTIVATION);
        //修改
        shopMapper.updateByPrimaryKeySelective(shop);
        return JsonResult.createSuccess();
    }

    @Override
    public void Update(Shop shop) {
        //修改状态
        shop.setState(PetConstant.ShopConstant.GET_OUT);
        shopMapper.updateByPrimaryKeySelective(shop);
    }

    @Override
    public void updateState(Shop shop) {
        //查询数据修改状态
        shop.setState(PetConstant.ShopConstant.ACTIVATION);
        shopMapper.updateByPrimaryKeySelective(shop);
    }
}
